package com.baic.ibase.service.mqtt.handler;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baic.ibase.service.mqtt.MqttMessageStrategy;
import com.mysql.cj.protocol.Message;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Component
public class MqttMessageHandlerStrategy {

    private final List<MqttMessageStrategy> strategyList;

    public MqttMessageHandlerStrategy(List<MqttMessageStrategy> strategyList) {
        this.strategyList = strategyList;
    }

    public void handle(String topic, JSONObject message) {
        for (MqttMessageStrategy strategy : strategyList) {
            if (strategy.support(topic)) {
                strategy.handle(message);
                break;
            }
        }
    }

}
